/**
 * 代理商管理  提现申请
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>提现申请</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.deptName" placeholder="输入申请人员"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.deptNo" placeholder="输入申请代码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-coin" @click="openDialog()" v-if="btnShow">提现</el-button>
      </el-form-item>
	  <el-form-item>
	    <el-select size="small" v-model="formInline.state" placeholder="请选择" @change="selectChange">
			<el-option v-for="item in payType" :label="item.key" :value="item.value" :key="item.value"></el-option>
	    </el-select>
	  </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="申请人ID" width="200">
      </el-table-column>
      <el-table-column sortable prop="name" label="申请人名称" width="200">
      </el-table-column>
      <el-table-column sortable prop="image" label="申请人头像" width="200">
        <template slot-scope="scope">
          <img v-if="scope.row.image" :src="scope.row.image" class="avatar" />
        </template>
      </el-table-column>
      <el-table-column sortable prop="name" label="申请金额" width="100">
      </el-table-column>
      <el-table-column  label="操作" min-width="300">
        <template slot-scope="scope">
			    <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row,'受理')" v-if="scope.row.state==0">受理</el-button>
          <el-button size="mini" type="danger" @click="handleEdit(scope.$index, scope.row,'拒绝')" v-if="scope.row.state==1">拒绝</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row,'同意')" v-if="scope.row.state==1">同意</el-button>
		      <el-button type="text" v-if="scope.row.state==2">已完成</el-button>
		      <el-button type="text" disabled v-if="scope.row.state==3">已拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 提现页面 -->
		<el-dialog class="poup" title="提现" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
			<el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="银行卡" prop="bankCard">
          <el-select size="small" v-model="editForm.bankCard" placeholder="请选择" @change="bankCardChange">
            <el-option v-for="item in editForm.bankCardList" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
				</el-form-item>
				<el-form-item label="金额" prop="money">
          <el-input size="small" type="number" v-model="editForm.money" auto-complete="off" placeholder="请输入金额"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeDialog">取消</el-button>
				<el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
			</div>
		</el-dialog>
  </div>
</template>

<script>
import { getWithList, updateWith, addWith, getCardList } from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      editFormVisible: false, //控制提现页面显示与隐藏
      loading: false, //是显示加载
      editForm: {
				bankCard: '',
        bankCardList:[],
				money: ''
			},
      // rules表单验证
			rules: {
				bankCard: [{ required: true, message: '请选择银行卡', trigger: 'blur' }],
				money: [{ required: true, message: '请输入提现金额', trigger: 'blur' }]
			},
      formInline: {
        page: 1,
        pagenum: 10,
		    //state:null,
      },
      btnShow:false, //展示提现按钮
      listData: [], //用户数据
      payType: [
        { key: '请选择', value: '' },
        { key: '申请中', value: 0 },
        { key: '受理中', value: 1 },
        { key: '已完成', value: 2 },
        { key: '已拒绝', value: 3 }
      ],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.btnShow=JSON.parse(localStorage.getItem('userdata')).accounttype==1?true:false;
    this.getBankCardList()
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    //选择器改变事件
    selectChange(e){
      if(this.formInline.state!==0&&!this.formInline.state){
        delete this.formInline.state;
      }
      this.getdata(this.formInline)
    },
    // 获取当前用户银行卡列表
    getBankCardList(){
      let parameter={adminstratorID:JSON.parse(localStorage.getItem('userdata')).ID}
      getCardList(parameter).then(res => {
          // this.editForm.bankCardList = res.date;
          this.editForm.bankCardList = [{id:1,name:'招商银行',number:'4613121215454'},{id:2,name:'邮政银行',number:'4613121215454'},{id:3,name:'招商银行',number:'4613121215454'}];
        }).catch(err => {
          this.$message.error('获取银行卡信息失败，请稍后再试！')
        })
    },
    // 获取列表
    getdata(parameter) {
      this.loading = true
      getWithList(parameter).then(res => {
          this.loading = false
          this.$message({
            type: 'info',
            message: res.message
          })
          this.listData = res.date;
          // 分页赋值
          // this.pageparm.currentPage = this.formInline.page
          // this.pageparm.pageSize = this.formInline.limit
          this.pageparm.total = res.count
        }).catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.pagenum = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    // 关闭提现弹出框
		closeDialog() {
			this.editFormVisible = false;
		},
    //显示提现弹出框
		openDialog: function(index) {
			this.editFormVisible = true;
		},
    //选择器改变事件
    bankCardChange(e){
      console.log(e)
      console.log(this.editForm.bankCard)
    },
    // 申请提现方法
		submitForm(editData) {
      this.loading = true;
      let parameter={backcardID:this.editForm.bankCard,money:this.editForm.money}
			this.$refs[editData].validate(valid => {
				if (valid) {
					addWith(parameter).then(res => {
						this.editFormVisible = false;
						this.loading = false;
						this.getdata(this.formInline);
						this.$message({
							type: 'success',
							message: '提现成功！'
						});
					}).catch(err => {
						this.editFormVisible = false;
						this.loading = false;
					});
				} else {
					return false;
				}
			});
		},
    // 修改状态
    handleEdit: function(index, row,type) {
      this.$confirm('确定要'+type+'吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let parameter={state:row.state,withdrawalID:row.withdrawalID};
        updateWith(parameter).then(res => {
          this.getdata(this.formInline)
        }).catch(err => {
          this.loading = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消拒绝'
        })
      })
    },
  }
}
</script>

<style scoped lang="less">
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.avatar {
	width: 80px;
	height: 80px;
	display: block;
}
.avatar-uploader {
	.el-upload {
		border: 1px dashed #8c939d;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}
	.el-upload:hover {
		border-color: #409eff;
	}
}
.avatar-uploader-icon {
	border: 1px dashed #8c939d;
	font-size: 28px;
	color: #8c939d;
	width: 80px;
	height: 80px;
	line-height: 80px;
	text-align: center;
}
</style>

 